import { _decorator, Component, Node, BoxCollider, ColliderComponent, ITriggerEvent } from 'cc';
import { constant } from '../constant';
import { gameManeger } from '../framework/gameManeger';
import { PoolManager } from '../framework/PoolManager';
const { ccclass, property } = _decorator;
const DISTANCEZ = -40;//z轴固定距离
@ccclass('boss')
export class boss extends Component {
    @property public createBullTime = 0.8;//子弹发射周期
    @property public bossHp = 100;
    public enemySpeed = 0;

    private _curBulletTime = 0;
    private curPosZ = 0;
    private _gameManeger: gameManeger = null;
    onEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.on("onTriggerEnter", this._onTriggerEnter, this);
    }
    onDisEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.off("onTriggerEnter", this._onTriggerEnter, this);
    }
    start() {

    }

    update(deltaTime: number) {
        const pos = this.node.position;
        if (this.curPosZ < DISTANCEZ) {
            this.curPosZ = pos.z + this.enemySpeed;
            this.node.setPosition(pos.x, pos.y, this.curPosZ);
        }
        this._curBulletTime += deltaTime;
        if (this._curBulletTime > this.createBullTime) {
            this._curBulletTime = 0;
            this._gameManeger.createBossMissile(this.node.position);
        }

        // if (movDistance > OUTOFSCREEN) {
        //     PoolManager.instance().putNode(this.node);
        //     //this.node.destroy();
        //     //console.log("dijiyijingchujie");
        // }
    }
    private _onTriggerEnter(event: ITriggerEvent) {
        const group = event.otherCollider.getGroup();
        if (group == constant.CollisionType.PLAYER_BULLET) {
            this.bossHp--;
            if (this.bossHp < 0) {
                PoolManager.instance().putNode(this.node);
            }
            //this._gameManeger.addScore();
            //this._gameManeger.createEffect(this.node.position);
            //this.node.destroy();
        }
    }
    show(_gameManeger: gameManeger, speed: number) {//为飞机设置速度
        this.enemySpeed = speed;
        this._gameManeger = _gameManeger;
    }
}

